$(document).ready(function(){
	load_color();
	activecolour();
	colourshover();
	forIpad();
	colourboard();
//Fill colours
$('#print').click(function(){
    var el = $('#colonia').get(0); 
    html2canvas.Preload(el, {
        complete: function(image){
            var queue = html2canvas.Parse(el, image, {elements: el}),
                $canvas = $(html2canvas.Renderer(queue, {elements: el}));
            saveData($canvas[0].toDataURL());
        }
    });
});
	$("#cover_metal_color_selection .colours").click(function(){
		colourshover();
		$("#color_bond_roof").css("display","block");
		$("#roof_tiles").css("display","none");
		$(this).parent().find(".active").removeClass("active");
		$(this).addClass("active");
		var colours = $(this).attr("title").toLowerCase().replace(" ","");
		$("#color_bond_roof").removeClass().addClass("overlay "+colours);
		$("#cover_tile_color_selection").find(".active").removeClass("active");
		$("#cover_tile_color_selection .cover_tile_color_selection").html("");
		$("body #h_roof").html("RoofMetal_"+colours);
	});
	
	$("#cover_tile_color_selection .colours").click(function(){
		colourshover();
		$("#roof_tiles").css("display","block");
		$("#color_bond_roof").css("display","none");
		$(this).parent().find(".active").removeClass("active");
		$(this).addClass("active");
		var colours = $(this).attr("title").toLowerCase().replace(" ","");
		$("#roof_tiles").removeClass().addClass("overlay "+colours);
		$("#cover_metal_color_selection").find(".active").removeClass("active");
		$("#cover_metal_color_selection .cover_metal_color_selection").html("");
		$("body #h_roof").html("RoofTile_"+colours);
	});

	$("#gutter_fascia_color_selection .colours").click(function(){
		var colours = fillcolours(this,"#gutter_fascia");
		$("#h_gutter").html("Gutter_"+colours);
	});
	
	$("#windowframes_color_selection .colours").click(function(){
		var colours = fillcolours(this,"#window_frames");
		$("body #h_windowframes").html("WindowFrame_"+colours);
	});
	
	$("#garage_door_color_selection .colours").click(function(){
		var colours = fillcolours(this,"#garage_doors");
		$("body #h_garage").html("Garage_"+colours);
	});
	
	$("#paving_color_selection .colours").click(function(){
		var colours = fillcolours(this,"#driveway_paving");
		$("body #h_paving").html("Paving_"+colours);
	});
	
	$("#frontdoor_color_selection .colours").click(function(){
		var colours = fillcolours(this,"#front_door");
		$("body #h_front_door").html("FrontDoor_"+colours);
	});
	$("#barge_scribe_color_selection .colours").click(function(){
		var colours = fillcolours(this,"#barge_scribe");
		$("body #h_barge_scribe").html("BargeScribe_"+colours);
	});
	$("#gableinfill_color_selection .colours").click(function(){
		var colours = fillcolours(this,"#gable_infill");
		$("body #h_gable").html("Gable_"+colours);
	});
	$("#feature_wall_color_selection .colours").click(function(){
		var colours = fillcolours(this,"#feature_wall");
		$("body #h_feature_wall").html("FeatureWall_"+colours);
	});
	$("#main_render_color_selection .colours").click(function(){
		var colours = fillcolours(this,"#main_render");
		$("body #h_main_render").html("MainRender_"+colours);
	});
	$("#select_left .section").click(function(){
		$("#select_left").children().removeClass("active");
		$("#select_right").find(".active").removeClass("active");
		$(this).addClass("active");
		$("#color_selection .color_selection_house").removeClass().addClass("color_selection_house");
		if ($("#select_left .active").attr("id") == 'roof_metal_b'){
			$("#cover_metal_color_selection").addClass("active");
		}
		if ($("#select_left .active").attr("id") == 'roof_tiles_b'){
			$("#cover_tile_color_selection").addClass("active");
		}
		if ($("#select_left .active").attr("id") == 'gutter_b'){
			$("#gutter_fascia_color_selection").addClass("active");
		}
		if ($("#select_left .active").attr("id") == 'barge_scribe_b'){
			$("#barge_scribe_color_selection").addClass("active");
		}
		if ($("#select_left .active").attr("id") == 'gable_infill_b'){
			$("#gableinfill_color_selection").addClass("active");
		}
		if ($("#select_left .active").attr("id") == 'barge_sribe'){
			$("#barge_sribe_color_selection").addClass("active");
		}
		if ($("#select_left .active").attr("id") == 'main_render_b'){
			$("#main_render_color_selection").addClass("active");
		}
		if ($("#select_left .active").attr("id") == 'wall_b'){
			$("#feature_wall_color_selection").addClass("active");
		}
			
	});
	$("#select_right .section").click(function(){
		$("#select_right").children().removeClass("active");
		$("#select_left").find(".active").removeClass("active");
		$(this).addClass("active");
		$("#color_selection .color_selection_house").removeClass().addClass("color_selection_house");
		if ($("#select_right .active").attr("id") == 'window_b'){
			$("#windowframes_color_selection").addClass("active");
		}
		if ($("#select_right .active").attr("id") == 'garage_b'){
			$("#garage_door_color_selection").addClass("active");
		}
		if ($("#select_right .active").attr("id") == 'paving_b'){
			$("#paving_color_selection").addClass("active");
		}

		if ($("#select_right .active").attr("id") == 'front_door_b'){
			$("#frontdoor_color_selection").addClass("active");
		}
		if ($("#select_right .active").attr("id") == 'wall_b'){
			$("#feature_wall_color_selection").addClass("active");
		}
	});
});

function colourshover(){
	$(".color_selection_house div").hover(function(){
		var cname = $(this).attr("title");
		var source = $(this).parent().attr("id");
		$("."+source).html(cname);
	},function(){
		var cname = $(this).parent().find(".active").attr("title");
		var source = $(this).parent().attr("id");
		$("."+source).html(cname);
	});

}

function colourboard(){
	var na_m = $(".cover_metal_color_selection").parent().find(".active").attr("title");
	$(".cover_metal_color_selection").html(na_m);
	var na_r = $(".cover_tile_color_selection").parent().find(".active").attr("title");
	$(".cover_tile_color_selection").html(na_r);
	var na_gu = $(".gutter_fascia_color_selection").parent().find(".active").attr("title");
	$(".gutter_fascia_color_selection").html(na_gu);
	var na_ba = $(".barge_scribe_color_selection").parent().find(".active").attr("title");
	$(".barge_scribe_color_selection").html(na_ba);
	var na_ba_sc = $(".barge_scribe_color_selection").parent().find(".active").attr("title");
	$(".barge_scribe_color_selection").html(na_ba_sc);
	var na_ba_sr = $(".barge_sribe_color_selection").parent().find(".active").attr("title");
	$(".barge_scribe_color_selection").html(na_ba_sr);
	var na_ma = $(".main_render_color_selection").parent().find(".active").attr("title");
	$(".main_render_color_selection").html(na_ma);
	var na_ga = $(".gableinfill_color_selection").parent().find(".active").attr("title");
	$(".gableinfill_color_selection").html(na_ga);
	var na_fd = $(".frontdoor_color_selection").parent().find(".active").attr("title");
	$(".frontdoor_color_selection").html(na_fd);
	var na_wf = $(".windowframes_color_selection").parent().find(".active").attr("title");
	$(".windowframes_color_selection").html(na_wf);
	var na_ga_d = $(".garage_door_color_selection").parent().find(".active").attr("title");
	$(".garage_door_color_selection").html(na_ga_d);
	var na_pa = $(".paving_color_selection").parent().find(".active").attr("title");
	$(".paving_color_selection").html(na_pa);
	var na_fw = $(".feature_wall_color_selection").parent().find(".active").attr("title");
	$(".feature_wall_color_selection").html(na_fw);
}

function load_color(){
	if ($("body #h_paving").text() != ""){
		var paving = $("body #h_paving").text().split("_");	
		$("body #driveway_paving").removeClass().addClass("overlay").addClass(paving[1]);
	}
	if ($("body #h_windowframes").text() != ""){
		var windowframes = $("body #h_windowframes").text().split("_");
		$("body #window_frames").removeClass().addClass("overlay").addClass(windowframes[1]);
	}
	if ($("body #h_garage").text() != ""){
		var garage = $("body #h_garage").text().split("_");
		$("body #garage_doors").removeClass().addClass("overlay").addClass(garage[1]);
	}
	if ($("body #h_gutter").text() != ""){
		var gutter = $("body #h_gutter").text().split("_");
		$("body #gutter_fascia").removeClass().addClass("overlay").addClass(gutter[1]);
	}
	if ($("body #h_feature_wall").text() != ""){
		var feature_wall = $("body #h_feature_wall").text().split("_");
		$("body #feature_wall").removeClass().addClass("overlay").addClass(feature_wall[1]);
	}
	if ($("body #h_barge_scribe").text() != ""){
		var barge_scribe = $("body #h_barge_scribe").text().split("_");
		$("body #barge_scribe").removeClass().addClass("overlay").addClass(barge_scribe[1]);
	}
	if ($("body #h_gable").text() != ""){
		var gable = $("body #h_gable").text().split("_");
		$("body #gable_infill").removeClass().addClass("overlay").addClass(gable[1]);
	}
	if ($("body #h_front_door").text() != ""){
		var front_door = $("body #h_front_door").text().split("_");
		$("body #front_door").removeClass().addClass("overlay").addClass(front_door[1]);
	}
	if($("body #h_roof").text().indexOf("RoofTile_") != -1){
			var roof = $("body #h_roof").text().split("_");
			$("#color_bond_roof").css({"display":"none"});
			$("#roof_tiles").css({"display":"block"});
			$("#roof_tiles").removeClass().addClass("overlay").addClass(roof[1]);
		}
	else if ($("body #h_roof").text().indexOf("RoofMetal_") !=-1)
		{
			var roof = $("body #h_roof").text().split("_");
			$("#roof_tiles").css({"display":"none"});
			$("#color_bond_roof").css({"display":"block"});
			$("#color_bond_roof").removeClass().addClass("overlay").addClass(roof[1]);
		}
}

function fillcolours(source,target){
	var colour = $(source).attr("title").toLowerCase().replace(" ","");
	var colours = colour.replace(" ","");
	$(target).removeClass().addClass("overlay "+colours);
	$(source).parent().find(".active").removeClass("active");
	$(source).addClass("active");
	return colours;
}

function forIpad(){
	var deviceAgent = navigator.userAgent.toLowerCase();
    var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
    if (agentID) {
		$("#header").css({"display":"none"});
		$("#footer").css({"display":"none"});
		$("body").css({"background":"white"});
    }
}

function activecolour(){

	var roof_tile		= $("#roof_tiles").attr("class").split(" ");
	var roof_metal 		= $("#color_bond_roof").attr("class").split(" ");
	var garage_doors 	= $("#garage_doors").attr("class").split(" ");
	var gutter_fascia 	= $("#gutter_fascia").attr("class").split(" ");
	var window_frames 	= $("#window_frames").attr("class").split(" ");
	var driveway_paving = $("#driveway_paving").attr("class").split(" ");
	var feature_wall 	= $("#feature_wall").attr("class").split(" ");
	var front_door 		= $("#front_door").attr("class").split(" ");

	if ($("#color_bond_roof").css('display')== 'none'){
		$("#cover_metal_color_selection").find(".active").removeClass("active");
		$("#cover_tile_color_selection").find(".active").removeClass("active");
		$("#cover_tile_color_selection").find("div."+roof_tile[1]).addClass("active");
	}else if($("#roof_tiles").css('display')== 'none'){
		$("#cover_metal_color_selection").find(".active").removeClass("active");
		$("#cover_tile_color_selection").find(".active").removeClass("active");
		$("#cover_metal_color_selection").find("div."+roof_metal[1]).addClass("active");
	}
	
	$("#gutter_fascia_color_selection").find(".active").removeClass("active");
	$("#gutter_fascia_color_selection").find("div."+gutter_fascia[1]).addClass("active");
	
	$("#windowframes_color_selection").find(".active").removeClass("active");
	$("#windowframes_color_selection").find("div."+window_frames[1]).addClass("active");
	
	$("#garage_door_color_selection").find(".active").removeClass("active");
	$("#garage_door_color_selection").find("div."+garage_doors[1]).addClass("active");
	
	$("#paving_color_selection").find(".active").removeClass("active");
	$("#paving_color_selection").find("div."+driveway_paving[1]).addClass("active");

	$("#feature_wall_color_selection").find(".active").removeClass("active");
	$("#feature_wall_color_selection").find("div."+feature_wall[1]).addClass("active");
	
	$("#frontdoor_color_selection").find(".active").removeClass("active");
	$("#frontdoor_color_selection").find("div."+front_door[1]).addClass("active");
	
	if ($("#barge_scribe_color_selection").length){
	var barge_scribe	= $("#barge_scribe").attr("class").split(" ");
	$("#barge_scribe_color_selection").find(".active").removeClass("active");
	$("#barge_scribe_color_selection").find("div."+barge_scribe[1]).addClass("active");
	}
	if ($("#gableinfill_color_selection").length){
	var gable_infill	= $("#gable_infill").attr("class").split(" ");
	$("#gableinfill_color_selection").find(".active").removeClass("active");
	$("#gableinfill_color_selection").find("div."+gable_infill[1]).addClass("active");
	}
}
//save click
function save_click(id){
		var roof= $("body #h_roof").text() ;
		var gutter=$("body #h_gutter").text() ;
		var barge_scribe=$("body #h_barge_scribe").text() ;
		var garage=$("body #h_garage").text() ;
		var main_render=$("body #h_main_render").text() ;
		var paving=$("body #h_paving").text() ;
		var windowframes=$("body #h_windowframes").text() ;
		var gable=$("body #h_gable").text();
		var front_door=$("body #h_front_door").text() ;
		var feature_render=$("body #h_feature_render").text() ;
		var feature_wall=$("body #h_feature_wall").text() ;
		var kind = 1;
			$.ajax({
				url: "ajax.php",
				data:{kind:kind,id:id,roof:roof,gutter:gutter,barge_scribe:barge_scribe,garage:garage,main_render:main_render,paving:paving,windowframes:windowframes,gable:gable,front_door:front_door,feature_render:feature_render,feature_wall:feature_wall},
				type:"POST",
				dataType:"html",
				success:function(data){
					if (data == 1){jAlert('You are successfull', 'Thank you');}
					else alert("Sever is busy...Please try again late!");
				}
			});
	}

//print_pdf
function print_pdf(id){
		var roof			= $("body #h_roof").text();
		var gutter			= $("body #h_gutter").text();
		var barge_scribe	= $("body #h_barge_scribe").text();
		var garage			= $("body #h_garage").text();
		var main_render		= $("body #h_main_render").text();
		var paving			= $("body #h_paving").text();
		var windowframes	= $("body #h_windowframes").text();
		var gable			= $("body #h_gable").text();
		var front_door		= $("body #h_front_door").text();
		var feature_wall	= $("body #h_feature_wall").text();
		var kind = 2;
		if (gutter != ""){
			gutter = gutter.split("_");
			gutter = gutter[1];
		}
		if (barge_scribe != ""){
			barge_scribe = barge_scribe.split("_");
			barge_scribe = barge_scribe[1];
		}
		if (garage != ""){
			garage = garage.split("_");
			garage = garage[1];
		}
		if (main_render != ""){
			main_render = main_render.split("_");
			main_render = main_render[1];
		}
		if (paving != ""){
			paving = paving.split("_");
			paving = paving[1];
		}
		if (windowframes != ""){
			windowframes = windowframes.split("_");
			windowframes = windowframes[1];
		}
		if (gable != ""){
			gable = gable.split("_");
			gable = gable[1];
		}
		if (front_door != ""){
			front_door = front_door.split("_");
			front_door = front_door[1];
		}
		if (feature_wall != ""){
			feature_wall=feature_wall.split("_");
			feature_wall = feature_wall[1];
		}
		var path=location.href.substr(0,((location.href).lastIndexOf("/")));
		var url = path+"/printpdf.php?id="+id+"&kind="+kind+"&roof="+roof+"&gutter="+gutter+"&barge_scribe="+barge_scribe+"&garage="+garage+"&=main_render"+main_render+"&paving="+paving+"&windowframes="+windowframes+"&gable="+gable+"&front_door="+front_door+"&feature_wall="+feature_wall;
		window.open(url);
	}